<?php

declare(strict_types=1);
/**
 * 
 */
namespace WanZ\App\User\Service;

use Hyperf\Database\Model\Builder;
use WanZ\App\User\Contract\UserBaseContract;
use WanZ\App\User\Model\UserBase;
use MoChat\Framework\Service\AbstractService;

class UserBaseService extends AbstractService implements UserBaseContract
{
    /**
     * @var UserBase
     */
    protected $model;
    
    /**
     * @var DEFAULT_FIELDS
     */
    private const DEFAULT_FIELDS = ['user_id', 'phone', 'created_at'];

    /**
     * 查询单条 - 根据用户ID.
     * @param int $userId 用户ID
     * @param array|string[] $columns 查询字段
     * @return null|\App\Model\UserBase|\Hyperf\Database\Model\Builder|object
     */
    public function getUserBaseByUserId(int $userId, array $columns = [])
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        return $this->model::query()->where('user_id', $userId)->first($columns);
    }

    /**
     * 查询单条 - 根据手机号.
     * @param string $phone 手机号
     * @param array $columns 查询字段
     * @return null|\App\Model\UserBase|\Hyperf\Database\Model\Builder|object
     */
    public function getUserBaseByPhone(string $phone, array $columns = [])
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        return $this->model::query()->where('phone', $phone)->first($columns);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $userId 用户ID
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateUserBaseByUserId(int $userId, array $data): int
    {
        return $this->model::query()->where('user_id', $userId)->update($data);
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createUserBase(array $data): int
    {
        return $this->model->createOne($data);
    }
    
}
